//
//  MARouteLineVC.m
//  YunGui
//
//  Created by HanenDev on 16/5/12.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "MARouteLineVC.h"

#define  ROUND 8.0f
#define STARTX [UIView getWidth:20]

@interface MARouteLineVC ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * _tableView;
}
@end

@implementation MARouteLineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    self.title=@"详细路线";
    self.navigationItem.leftBarButtonItem=[ViewTool getBackBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    
    [self createView];
    
}
- (void)createView{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(STARTX, (50-26)/2, 26, 26)];
    imageView.image=[UIImage imageNamed:@"Btn_Push_Qidian"];
    [headerView addSubview:imageView];
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(imageView.maxX+5, 5, kScreenWidth-imageView.maxX, 40)];
    label.text=[NSString stringWithFormat:@"起点 (%@)",self.startStr];
    [headerView addSubview:label];
    _tableView.tableHeaderView=headerView;
    
    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    UIImageView *picView=[[UIImageView alloc]initWithFrame:CGRectMake(STARTX, (50-26)/2, 26, 26)];
    picView.image=[UIImage imageNamed:@"Btn_Push_Daoda"];
    [footerView addSubview:picView];
    
    UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(picView.maxX+5, 5, kScreenWidth-imageView.maxX, 40)];
    lab.text=[NSString stringWithFormat:@"终点 (%@)",self.endStr];
    [footerView addSubview:lab];
    
    
    _tableView.tableFooterView=footerView;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_type == 2222) {
        return self.routeArray.count - 1;
    }
    return self.routeArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 2222) {
        return 80;
    }
    NSString *content=[self.routeArray[indexPath.row] instruction];
    CGFloat height=[self getHeightByString:content withFont:[UIFont systemFontOfSize:15.0f]];
    
    return height+20.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 2222) {
        static NSString *identifier=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
            for (int i=0; i<3; i++) {
                UILabel *label=[[UILabel alloc]init];
                label.tag=2000+i;
                label.font = [UIFont systemFontOfSize:15.0f];
                [cell addSubview:label];
                
                UIImageView *imgView = [[UIImageView alloc]init];
                imgView.tag = 3000+i;
                imgView.image = [UIImage imageNamed:@"Btn_Push_Guocheng"];
                [cell addSubview:imgView];
            }
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        UILabel *label1=[cell viewWithTag:2000];
        UILabel *label2=[cell viewWithTag:2001];
        UILabel *label3=[cell viewWithTag:2002];
        
        UIImageView *imgView1=[cell viewWithTag:3000];
        UIImageView *imgView2=[cell viewWithTag:3001];
        UIImageView *imgView3=[cell viewWithTag:3002];
        
        label1.frame = CGRectMake(STARTX + 20+11, 5, kScreenWidth - 70, 20);
        label2.frame = CGRectMake(STARTX + 20+11, label1.maxY+5, kScreenWidth - 70, 20);
        label3.frame = CGRectMake(STARTX + 20+11, label2.maxY+5, kScreenWidth - 70, 20);
        imgView1.frame = CGRectMake(STARTX +8, 5+(label1.height-10)/2, 10, 10);
        imgView2.frame = CGRectMake(STARTX +8, label2.y+(label1.height-10)/2, 10, 10);
        imgView3.frame = CGRectMake(STARTX +8, label3.y+(label1.height-10)/2, 10, 10);
        
        
        AMapSegment *segment = self.routeArray[indexPath.row];
        for (int j=0; j<segment.buslines.count; j++) {
            AMapBusLine *bus = segment.buslines[j];
            label1.text = [NSString stringWithFormat:@"步行%ld米",(long)segment.walking.distance];
            label2.text = [NSString stringWithFormat:@"%@ 上车",bus.departureStop.name];
            label3.text = [NSString stringWithFormat:@"%@ 下车",bus.arrivalStop.name];
        }
        
        return cell;
        
    }else{
        static NSString *identifier=@"cell";
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil)
        {
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            
            UIImageView *imgView = [[UIImageView alloc]init];
            imgView.tag = 3000;
            [cell addSubview:imgView];
            
            UILabel *label=[[UILabel alloc]init];
            label.tag=2000;
            [cell addSubview:label];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        UILabel *label=[cell viewWithTag:2000];
        NSString *content=[self.routeArray[indexPath.row] instruction];
        label.text=content;
        label.numberOfLines=0;
        label.font=[UIFont systemFontOfSize:15.0f];
        CGFloat height=[self getHeightByString:content withFont:[UIFont systemFontOfSize:15.0f]];
        label.frame=CGRectMake(STARTX + 20+11, 10, kScreenWidth-70, height);
        
        UIImageView *imageV = [cell viewWithTag:3000];
        
        imageV.frame = CGRectMake(STARTX +8, 10+(label.height-10)/2, 10, 10);
        imageV.image = [UIImage imageNamed:@"Btn_Push_Guocheng"];
        
        
        return cell;

    }
    return nil;
}

-(CGFloat)getHeightByString:(NSString*)string   withFont:(UIFont*)font
{
    CGSize size = [string boundingRectWithSize:CGSizeMake(kScreenWidth-50, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return size.height ;
    
}


- (void)goToBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
